import 'dart:convert';

import 'package:flutter_chip/bean/Course.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SpUtils {
  static const SP_TOKEN = "sp_token";
  static const SP_USER = "sp_user";
  static const String _MCU = "SP_MCU";
  static const String SP_BLE = "SP_BLE_CONFIG";
  static const String SP_MOB = "SP_MOB_AGREE";
  static const String SP_MYTAB = "SP_MY_TABS";

  static void put(String key, var value) async {
    SharedPreferences spf = await SharedPreferences.getInstance();
    if (value is String) {
      spf.setString(key, value);
    } else if (value is int) {
      spf.setInt(key, value);
    } else if (value is double) {
      spf.setDouble(key, value);
    } else if (value is bool) {
      spf.setBool(key, value);
    } else if (value is List<String>) {
      spf.setStringList(key, value);
    }
  }

  static Future<dynamic> get(String key, var defaultValue) async {
    SharedPreferences spf = await SharedPreferences.getInstance();
    var value;
    if (defaultValue is String) {
      value = spf.getString(key);
    } else if (defaultValue is int) {
      value = spf.getInt(key);
    } else if (defaultValue is double) {
      value = spf.getDouble(key);
    } else if (defaultValue is bool) {
      value = spf.getBool(key);
    } else if (defaultValue is List<String>) {
      value = spf.getStringList(key);
    }
    if (null == value) return defaultValue;
    return value;
  }

  static Future<Null> clear() async {
    SharedPreferences spf = await SharedPreferences.getInstance();
    spf.remove(SP_TOKEN);
    spf.remove(SP_USER);
  }

  static void saveMcu(String key, String jsonMcu) async {
    SharedPreferences spf = await SharedPreferences.getInstance();
    spf.setString("${_MCU}_$key", jsonMcu);
  }

  static Future<List> getMcu(String key) async {
    SharedPreferences spf = await SharedPreferences.getInstance();
    if (spf.getString("${_MCU}_$key") == null) return null;
    return json
        .decode(spf.getString("${_MCU}_$key"))
        .map((map) => Course.fromJson(map))
        .toList();
  }

  static Future<Map<String, String>> getBleConfig() async {
    Map<String, String> config = Map();
    String configStr = await SpUtils.get(SpUtils.SP_BLE, "");
    if (configStr.isEmpty) {
      config.addAll(Comm.bleMap);
    } else {
      Map<String, dynamic> tmp = jsonDecode(configStr);
      config["dir_up"] = tmp["dir_up"] ?? Comm.bleMap["dir_up"];
      config["dir_down"] = tmp["dir_down"] ?? Comm.bleMap["dir_down"];
      config["dir_left"] = tmp["dir_left"] ?? Comm.bleMap["dir_left"];
      config["dir_right"] = tmp["dir_right"] ?? Comm.bleMap["dir_right"];
      config["dir_center"] = tmp["dir_center"] ?? Comm.bleMap["dir_center"];
      config["dir_label"] = tmp["dir_label"] ?? Comm.bleMap["dir_label"];

      config["recv_start1"] = tmp["recv_start1"] ?? Comm.bleMap["recv_start1"];
      config["recv_end1"] = tmp["recv_end1"] ?? Comm.bleMap["recv_end1"];
      config["label_recv1"] = tmp["label_recv1"] ?? Comm.bleMap["label_recv1"];
      config["recv_start2"] = tmp["recv_start2"] ?? Comm.bleMap["recv_start2"];
      config["recv_end2"] = tmp["recv_end2"] ?? Comm.bleMap["recv_end2"];
      config["label_recv2"] = tmp["label_recv2"] ?? Comm.bleMap["label_recv2"];

      config["label_1"] = tmp["label_1"] ?? Comm.bleMap["label_1"];
      config["label_2"] = tmp["label_2"] ?? Comm.bleMap["label_2"];
      config["label_3"] = tmp["label_3"] ?? Comm.bleMap["label_3"];
      config["label_4"] = tmp["label_4"] ?? Comm.bleMap["label_4"];
      config["label_5"] = tmp["label_5"] ?? Comm.bleMap["label_5"];
      config["label_6"] = tmp["label_6"] ?? Comm.bleMap["label_6"];
      config["label_7"] = tmp["label_7"] ?? Comm.bleMap["label_7"];
      config["label_8"] = tmp["label_8"] ?? Comm.bleMap["label_8"];
      config["label_9"] = tmp["label_9"] ?? Comm.bleMap["label_9"];
      config["send_1"] = tmp["send_1"] ?? Comm.bleMap["send_1"];
      config["send_2"] = tmp["send_2"] ?? Comm.bleMap["send_2"];
      config["send_3"] = tmp["send_3"] ?? Comm.bleMap["send_3"];
      config["send_4"] = tmp["send_4"] ?? Comm.bleMap["send_4"];
      config["send_5"] = tmp["send_5"] ?? Comm.bleMap["send_5"];
      config["send_6"] = tmp["send_6"] ?? Comm.bleMap["send_6"];
      config["send_7"] = tmp["send_7"] ?? Comm.bleMap["send_7"];
      config["send_8"] = tmp["send_8"] ?? Comm.bleMap["send_8"];
      config["send_9"] = tmp["send_9"] ?? Comm.bleMap["send_9"];
    }
    return config;
  }
}
